<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>购物车</title>
    <style>
      thead {
        background-color: #f5f5f5;
      }
      th,
      td {
        border: 1px solid #aaa;
        padding: 8px 12px;
        text-align: center;
      }
    </style>
  </head>
  <body>
    <table>
      <thead>
        <tr>
          <th>编号</th>
          <th>书籍名称</th>
          <th>出版日期</th>
          <th>价格</th>
          <th>购买数量</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody></tbody>
    </table>
    <h2 class="price">总价格:¥<span class="price-count">0</span></h2>
    <script>
      //1.先拿到数据
      let books = [
        {
          id: 1,
          name: '《算法导论》',
          date: '2006-09',
          price: 85.0,
          count: 3,
        },
        {
          id: 2,
          name: '《UNIX编程艺术》',
          date: '2006-02',
          price: 59.0,
          count: 2,
        },
        {
          id: 3,
          name: '《编程珠玑》',
          date: '2008-10',
          price: 39.0,
          count: 5,
        },
        {
          id: 4,
          name: '《代码大全》',
          date: '2006-03',
          price: 128.0,
          count: 8,
        },
      ];
      //2. 展示数据
      //对于到底是通过html编写，还是用js操作DOM来 创建元素
      //1. 对于固定的，直接通过html来编写，能通过html写的尽量用html
      //2. 对于大量的数据，有规律的 可以通过js编写
      let tbody = document.querySelector('tbody');
      // //2.2 动态添加tr以及内部的数据
      // for (let i = 0; i < books.length; i++) {
      //   //一个对象就是一个tr
      //   let tr = document.createElement('tr');
      //   //2.3 放具体的数据进去
      //   //单个的书对象
      //   let book = books[i];
      //   //获取所有的键名 id name date price count
      //   let bookKeys = Object.keys(book);
      //   //['id','name','date','price','count']
      //   //下一步是通过键名去拿到对应的键值
      //   for (let j = 0; j < bookKeys.length; j++) {
      //     //获取id
      //     let key = bookKeys[j];
      //     //拿到对应的value book['id']
      //     let value = book[key];
      //     let td = document.createElement('td');
      //     //如果键名是价格要做特殊处理
      //     if (key === 'price') {
      //       value = '¥' + value;
      //     }
      //     td.textContent = value;
      //     tr.append(td);
      //   }
      //   //添加删除按钮
      //   let delTd = document.createElement('td');
      //   let delBtn = document.createElement('button');
      //   delBtn.textContent = '删除';
      //   delTd.append(delBtn);
      //   tr.append(delTd);
      //   delBtn.onclick = function () {
      //     //删除对应的整个tr
      //     let delTr = this.parentElement.parentElement;
      //     //拿到要删除的索引
      //     let delTrIndex = delTr.sectionRowIndex;
      //     delTr.remove();
      //     //删除对应的数据
      //     books.splice(delTrIndex, 1);
      //     //重新计算价格 假设这个函数已经是写好的计算总价的
      //     calcTotalPrice();
      //   };
      //   tbody.append(tr);
      // }
      // //计算总价
      // let priceCount = document.querySelector('.price-count');
      // calcTotalPrice();
      // function calcTotalPrice() {
      //   let totalPrice = books.reduce(function (prev, ele) {
      //     return prev + ele.count * ele.price;
      //   }, 0);
      //   priceCount.textContent = totalPrice;
      // }

      books.forEach((ele) => {
        let tr = document.createElement('tr');
        //获取键名 键值
        for (const key in ele) {
          let td = document.createElement('td');
          let value = ele[key];
          if (key === 'price') {
            value = '¥' + ele[key];
          }
          td.textContent = value;
          tr.append(td);
        }
        //删除
        let delTd = document.createElement('td');
        let delBtn = document.createElement('button');
        delBtn.textContent = '删除';
        delTd.append(delBtn);
        tr.append(delTd);
        delBtn.onclick = (e) => {
          let delTr = e.target.parentElement.parentElement;
          let delTrIndex = delTr.sectionRowIndex;
          delTr.remove();
          books.splice(delTrIndex, 1);
          calc();
        };
        tbody.append(tr);
      });
      let priceCount = document.querySelector('.price-count');
      calc();
      function calc() {
        // let totalPrice = books.reduce(function (prev, ele) {
        //   return prev + ele.count * ele.price;
        // }, 0);
        // priceCount.textContent = totalPrice;
        priceCount.textContent = books.reduce(
          (prev, ele) => prev + ele.count * ele.price,
          0
        );
      }
    </script>
  </body>
</html>
